package com.cg.controller.map;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cg.controller.AbstractController;
import com.cg.entity.MapBuilding;
import com.cg.entity.MapData;
import com.cg.entity.MapGrid;
import com.cg.service.impl.MapBuildingService;
import com.cg.service.impl.MapGridService;
import com.cg.utils.Constant;
import com.cg.utils.Query;
import com.cg.utils.R;
import com.cg.utils.annotation.SysLog;
import com.google.gson.Gson;


/**
 * 地图网格数据
 * @author cumt
 *
 */
@RestController
@RequestMapping("/sys/mapgrid")
public class MapGridController extends AbstractController{
		
	@Autowired
	MapGridService mapGridService;
	
	@Autowired
	MapBuildingService mapBuildingService;
	
	Gson gson = new Gson();
	/**
	 * 所有列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:mapgrid:list")
	public R list(@RequestParam Map<String, Object> params){
		//只有超级管理员，才能查看所有管理员列表
		if(getUserId() != Constant.SUPER_ADMIN){
			params.put("createUserId", getUserId());
		}
		//查询列表数据
		Query query = new Query(params);
//		BaseBusiness business=new BaseBusiness();
		List<MapGrid> gridList = mapGridService.queryList(query);
		int total = mapGridService.queryTotal(query);
		
		//PageUtils pageUtil = new PageUtils(userList, total, query.getLimit(), query.getPage());
		
		return R.success(total, "", gridList);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("sys:mapgrid:info")
	public R info(@PathVariable("id") Long id){
		
		MapGrid mapgrid = mapGridService.queryObject(id);
		String str=MapData.replaceBlank(mapgrid.getPolygon());
		MapData data = gson.fromJson(str, MapData.class);
		mapgrid.setPolygon(String.valueOf(data.getData().get(0)));
		return R.ok().put("mapgrid", mapgrid);
	}
	
	
	
	/**
	 * 网格列表
	 */
	@RequestMapping("/select")
	@RequiresPermissions("sys:mapgrid:select")
	public R select(String gridId){
		
		Map<String, Object> params = new HashMap<>();
		List<MapGrid> list = mapGridService.queryList(params);
		
		R result = R.ok();
		if(list!=null && list.size()!=0) {
			if(gridId==null||"".equals(gridId)) {
				params.put("gridId", list.get(0).getId());
			}
			else {
				params.put("gridId", gridId);
			}
			
			List<MapBuilding> buildingList = mapBuildingService.queryList(params);
			
			result.put("buildingList", buildingList);
		}
		
		result.put("list", list);
		
		return result;
	}
	
	/**
	 * 保存用户
	 */
	@SysLog("保存")
	@RequestMapping("/save")
	@RequiresPermissions("sys:mapgrid:save")
	public R save(@RequestBody MapGrid mapgrid){
		
		MapData data = new MapData();
		List datalist =new ArrayList();
		datalist.add(mapgrid.getPolygon());
		data.setData(datalist);
		String polygon = gson.toJson(data);
		
		mapgrid.setPolygon(polygon);
		mapGridService.save(mapgrid);
		
		return R.ok();
	}
	/**
	 * 修改
	 */
	@SysLog("修改")
	@RequestMapping("/update")
	@RequiresPermissions("sys:mapgrid:update")
	public R update(@RequestBody MapGrid mapgrid){
		
//		comment.set(getUserId());
		MapData data = new MapData();
		List datalist =new ArrayList();
		datalist.add(mapgrid.getPolygon());
		data.setData(datalist);
		String polygon = gson.toJson(data);
		
		mapgrid.setPolygon(polygon);
		
		mapGridService.update(mapgrid);
		
		return R.ok();
	}
	
	/**
	 * 删除用户
	 */
	@SysLog("删除")
	@RequestMapping("/delete")
	@RequiresPermissions("sys:mapgrid:delete")
	public R delete(@RequestBody Long[] ids){
		
		mapGridService.deleteBatch(ids);
		
		return R.ok();
	}
	
	
	
	
	
	
}
